<?php
/**
 * fldcms
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-08-09
 */
namespace app\admin\controller;
use think\Request;
use think\helper\Time;
/**
 * 友情链接管理
 * @author fld
 *
 */
class LinkController extends BaseController{
	function __construct(){
		parent::__construct();
		$this->db = model('Link');
	}
	/**
	 * 友情链接列表
	 */
	public function indexOp(){
		$result = $this->getCondition();
		$list = $this->db->getLinkList($result);
		$this->assign("list",$list);
		// 获取分页显示
		$page = $list->render();
		$this->assign('page', $page);
		return $this->fetch();
	}
	
	public function getCondition(){
		$result = array();
		$condition = array();
		$query = array();
		//关键字
		$keyword = input('param.keyword/s');
		if(!empty($keyword)){
			$condition['link_name|link_url'] = array('like',$keyword);
			$query['keyword'] = $keyword;
		}
		$this->assign('keyword',$keyword);
		$result['condition'] = $condition;
		$result['query'] = $query;
		return $result;
	}
	/**
	 * 添加友情链接信息
	 */
	public function addOp(){
	    if (chksubmit()){
	    	$data = input('post.');
	    	$res = $this->db->addData($data);
	        if($res !== false){
	        	model('ActionLog')->action_log("添加","添加友情链接成功，友情链接编号=".$res."，友情链接名称=".$data['link_name']);
	            $this->msg = "添加友情链接成功";
	            $result['error'] = 0;
	        }else{
	            $this->msg = "添加友情链接失败";
	            $result['error'] = 1;
	        }
	        $result['msg'] = $this->msg;
	        return json($result);
	    }else{
	        return $this->fetch();
	    }
	}
	/**
	 * 编辑友情链接信息
	 */
	public function editOp(){
		if (chksubmit()){
			$data = input('post.');
	    	$res = $this->db->updateData($data);
	        if($res !== false){
	        	model('ActionLog')->action_log("编辑","编辑友情链接成功，友情链接编号=".$data['id']);
	            $this->msg = "编辑友情链接成功";
	            $result['error'] = 0;
	        }else{
	            $this->msg = "编辑友情链接失败";
	            $result['error'] = 1;
	        }
	        $result['msg'] = $this->msg;
	        return json($result);
		}else{
			//友情链接信息
			$id = input('param.id');
			$info = $this->db->getInfo(array('id'=>$id));
			$this->assign('vo',$info);
			return $this->fetch();
		}
	}
	/**
	 * 删除友情链接信息
	 */
	public function deleteOp(){
		$result = array();
		$id = input('param.id');
		$info = $this->db->getInfo(array('id'=>$id));
		$res = $this->db->deleteData(array('id'=>$id));
		if($res !== false){
			model('ActionLog')->action_log("删除","删除友情链接成功，友情链接编号=".$info['id']."，网站名称=".$info['link_name']."，网站地址=".$info['link_url']);
		    $result['error'] = 0;
		}else{
			$this->msg = "删除友情链接失败";
			$result['error'] = 1;
		}
		$result['msg'] = $this->msg;
		return json($result);
	}
	
	/**
	 * 检查友情链接名是否存在
	 */
	public function check_ajaxOp(){
		$link_name = input('post.link_name');
		$info = $this->db->getInfo(array('link_name'=>trim($link_name)));
		if($info){
			return json(false);
		}else{
			return json(true);
		}
	}
	/**
	 * 检查友情链接名是否存在
	 */
	public function check_name_ajaxOp(){
		$link_name = input('post.link_name');
		$id = input('post.id');
		$condtion = array();
		$condtion['link_name'] = trim($link_name);
		$condtion['id'] = array('neq',$id);
		$info = $this->db->getInfo($condtion);
		if($info){
			return json(false);
		}else{
			return json(true);
		}
	}
	
	public function check_url_ajaxOp(){
		$link_url = input('post.link_url');
		$id = input('post.id');
		$condtion = array();
		$condtion['link_url'] = trim($link_url);
		if(!empty($id)){
			$condtion['id'] = array('neq',$id);
		}
		$info = $this->db->getInfo($condtion);
		if($info){
			return json(false);
		}else{
			return json(true);
		}
	}
	
}